import { Component, EventEmitter, Input, Output, OnInit, OnChanges } from '@angular/core';
import { Assembly, AssemblyStyles, AssemblyValidators } from '../../../fb-design.model'
import { DynamicDialogRef, DynamicDialogConfig } from 'primeng/api';

@Component({
    templateUrl: 'qk-item-attributes.component.html',
    styleUrls: ['qk-item-attributes.component.scss'],
    providers: []
})
export class QkItemAttributes implements OnInit {
    items: any[];
    cols: any[] = [
        { field: 'label', header: '标签' },
        { field: 'value', header: '键值' },
        { field: 'displayOrder', header: '排序', width: '120px' }
    ];
    constructor(
        public ref: DynamicDialogRef,
        public config: DynamicDialogConfig) { }

    ngOnInit() {
        let its = this.config.data.items;
        its = JSON.parse(its);
        this.items = its.sort(function (i1, i2) {
            return i1.displayOrder - i2.displayOrder;
        });
    }

    save() {
        let its = [];
        this.items.forEach(item => {
            its.push(item);
        });
        its = its.sort(function (i1, i2) {
            return i1.displayOrder - i2.displayOrder;
        });
        this.ref.close(its);
    }

    cancel() {
        this.ref.close();
    }

    deleteItem(item: any) {
        var temp = [];
        this.items.forEach(element => {
            if (element.value != item.value)
                temp.push(element);
        });
        this.items = temp;
    }

    addItem($event: any) {
        this.items.push({ 'displayOrder': this.items.length + 1 });
    }


}